package com.gmall.product.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.gmall.model.product.BaseCategoryTrademark;
import com.gmall.model.product.BaseTrademark;
import com.gmall.model.product.dto.BaseCategoryTrademarkDTO;
import com.gmall.product.mapper.BaseCategoryTrademarkMapper;
import com.gmall.product.mapper.BaseTrademarkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BaseCategoryTrademarkService {

    @Autowired
    BaseCategoryTrademarkMapper baseCategoryTrademarkMapper;

    @Autowired
    BaseTrademarkMapper  baseTrademarkMapper;


    public List<BaseTrademark> findBaseTrademarkListByCategory3Id(Long category3Id){
        //Mybatis-plus MP
        //select * from base_category_trademark where category3_id = ?
        /**
         * SELECT * from base_category_trademark where category3_id = 192 and is_deleted = 0
         */
        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);

        List<Long> ids = baseCategoryTrademarkList.stream()
                .map(baseCategoryTrademark -> {
                    return baseCategoryTrademark.getTrademarkId();
                }).collect(Collectors.toList());

        /**
         * select * from base_trademark where id in (1,2,3)
         */

        QueryWrapper<BaseTrademark> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().in(ids.size()>0,BaseTrademark::getId,ids);
        List<BaseTrademark> baseTrademarks = baseTrademarkMapper.selectList(queryWrapper1);

        return baseTrademarks;
    }

    public List<BaseTrademark> findBaseTrademarkListByCategory3Id1(Long category3Id){

        return baseCategoryTrademarkMapper.findBaseTrademarkListByCategory3Id(category3Id);
    }

    public List<BaseTrademark> findNotExistBaseTrademarkListByCategory3Id(Long category3Id) {

        /**
         * SELECT * from base_trademark where id not in(
         * SELECT trademark_id from base_category_trademark where category3_id = 192 and is_deleted = 0)
         * and is_deleted = 0
         */

        QueryWrapper<BaseCategoryTrademark> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(BaseCategoryTrademark::getCategory3Id,category3Id);
        List<BaseCategoryTrademark> baseCategoryTrademarkList = baseCategoryTrademarkMapper.selectList(queryWrapper);

        List<Long> ids = baseCategoryTrademarkList.stream()
                .map(baseCategoryTrademark -> {
                    return baseCategoryTrademark.getTrademarkId();
                }).collect(Collectors.toList());

        /**
         * select * from base_trademark where id in (1,2,3)
         */

        QueryWrapper<BaseTrademark> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.lambda().notIn(ids.size()>0,BaseTrademark::getId,ids);
        List<BaseTrademark> baseTrademarks = baseTrademarkMapper.selectList(queryWrapper1);

        return baseTrademarks;
    }

    public void save(BaseCategoryTrademarkDTO baseCategoryTrademarkDTO) {

        Long category3Id = baseCategoryTrademarkDTO.getCategory3Id();
        List<BaseTrademark> trademarkList = baseCategoryTrademarkDTO.getTrademarkList();

        trademarkList.forEach((baseTrademark)->{
            Long baseTrademarkId = baseTrademark.getId();

            BaseCategoryTrademark baseCategoryTrademark = new BaseCategoryTrademark();
            Date now = new Date();
            baseCategoryTrademark.setCreateTime(now);
            baseCategoryTrademark.setUpdateTime(now);
            baseCategoryTrademark.setCategory3Id(category3Id);
            baseCategoryTrademark.setTrademarkId(baseTrademarkId);

            baseCategoryTrademarkMapper.insert(baseCategoryTrademark);
        });
    }

    public void remove(Long category3Id, Long trademarkId) {

        //delete from base_category_trademark where category3_id = ? and trademark_id = ?

        UpdateWrapper<BaseCategoryTrademark> updateWrapper = new UpdateWrapper<>();
        updateWrapper
                .lambda()
                .eq(BaseCategoryTrademark::getCategory3Id,category3Id)
                .eq(BaseCategoryTrademark::getTrademarkId,trademarkId);

        baseCategoryTrademarkMapper.delete(updateWrapper);

    }
}
